package com.tjetc.service.impl;

import com.tjetc.dao.UserMapper;
import com.tjetc.domain.User;
import com.tjetc.repository.UserDao;
import com.tjetc.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;
    @Resource
    private UserMapper userMapper;
    @Override
    public List<User> findUserByUserId(Integer userId, Integer receiverId) {
        List<User> users=new ArrayList<>();
        Optional<User> user1=userDao.findById(userId);
        if (user1.isPresent()){
//            System.out.println("Es查到用户1数据了");
            users.add(user1.get());
            Optional<User> user2=userDao.findById(receiverId);
            if (user2.isPresent()){
//                System.out.println("Es查到用户2数据了");
                users.add(user2.get());
            }else {
//                System.out.println("数据库开始查数据2");
                User user3=userMapper.selectByPrimaryKey(receiverId);
                users.add(user3);
                userDao.save(user3);
            }
        }else {
//            System.out.println("数据库开始查数据1");

            User user4=userMapper.selectByPrimaryKey(userId);
            users.add(user4);
            userDao.save(user4);
            Optional<User> user2=userDao.findById(receiverId);
            if (user2.isPresent()){
                users.add(user2.get());
            }else {
                User user3=userMapper.selectByPrimaryKey(receiverId);
                users.add(user3);
                userDao.save(user3);
            }
        }
        return users;
    }

    @Override
    public User getUserByPhone(String phone) {
        return userDao.findByPhone(phone);
    }

    @Override
    public List<String> findOther(Integer userId) {
       User user= userDao.findById(userId).get();
       String avatar=user.getAvatar();
       String username=user.getUsername();
       List<String> list=new ArrayList<>();
       list.add(avatar);
       list.add(username);
        return list;
    }
}
